package util;

import java.sql.*;

public class DBUtil {
    private static final String URL="jdbc:mysql://localhost:3306/testdatabase1?useUnicode=true&characterEncoding=gbk";
    public static final String DRIVER="com.mysql.jdbc.Driver";
    public static final String USER_NAME="root";
    public static final String PWD="hewenhui";
    static {
        try {
            Class.forName(DRIVER);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //获取数据库连接
    public static Connection getConn(){
        try{
            return DriverManager.getConnection(URL,USER_NAME,PWD);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }


    public static void closeConn(Connection connection){
        if(connection!=null){
            try{
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePS(PreparedStatement ps) {
        if(ps!=null){
            try{
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeRs(ResultSet Rs){
        if(Rs!=null){
            try{
                Rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
